package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public R deleteById(Long id) {
        //首先判断当前分类下是否有菜品
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();

        qw.eq(Dish::getCategoryId,id);

        Integer count = dishMapper.selectCount(qw);

        if (count>0){
            return R.error("当前分类下有菜品,不能删除");
        }

        //判断分类下是否有套餐
        LambdaQueryWrapper<Setmeal> qw1 = new LambdaQueryWrapper<>();

        qw1.eq(Setmeal::getCategoryId,id);

        Integer count1 = setmealMapper.selectCount(qw1);

        if (count1>0){
            return R.error("当前分类下有套餐,不能删除");
        }

        //调用service删除分类
        int i = categoryMapper.deleteById(id);

        return i>0? R.success("删除成功"): R.error("删除失败");
    }

}
